#version 330

out vec4 oColor;

smooth in vec3 oPos;
smooth in vec3 oTangent;
smooth in vec4 texPos;

uniform vec3 lightColor;
uniform vec3 lightPos;
uniform vec3 eyePos;
uniform mat4 inverseModelMat;
uniform sampler2D DOM;
uniform sampler2D depthMap;
uniform sampler2D MarschnerM;
uniform sampler2D MarschnerN;

void main(void)
{
	vec4 newTexPos = texPos / texPos.w;
	float depth = texture(depthMap, newTexPos.xy).r;
	vec4 DOMValue = texture(DOM, newTexPos.xy);
	
	float dis = newTexPos.z - depth;
	float opacity;
	float layer1 = 0.05, layer2 = 0.1, layer3 = 0.2;
	
	if(dis < layer1)
	{
		opacity = dis / layer1 * DOMValue.r;
		oColor.rgb = vec3(1.0, 0.0, 0.0);
	}
	else if(dis < layer2)
	{
		opacity = (dis - layer1) / (layer2 - layer1)*DOMValue.g + (layer2 - dis)/(layer2 - layer1)*DOMValue.r;
		oColor.rgb = vec3(0.0, 1.0, 0.0);
	}
	else
	{
		opacity = (dis - layer2)/(layer3 - layer2)*DOMValue.z + (layer3 - dis)/(layer3 - layer2)*DOMValue.y;
		oColor.rgb = vec3(0.0, 0.0, 1.0);
	}
	 
	float specular, diffuse;
	vec3 t = normalize(oTangent);
	
	vec4 newLightPos = vec4(lightPos, 1.0);
	vec4 newEyePos = vec4(eyePos, 1.0);
	
	newLightPos = inverseModelMat * newLightPos;
	newEyePos = inverseModelMat * newEyePos;
	
	vec3 l = normalize(newLightPos.xyz - oPos);
	vec3 e = normalize(newEyePos.xyz - oPos);
	
	//t = -t;
	
	float sinThetaI = (dot(l, t));
	float sinThetaO = (dot(e, t));
	
	vec2 uv1;
	uv1.r = dot(l, t) * 0.5 + 0.5;
	uv1.g = dot(e, t) * 0.5 + 0.5;
	
	vec3 lightPerp = l - dot(l, t) * t;
	vec3 eyePerp = e - dot(e, t) * t;
	
	float cosPhiD = dot(lightPerp, eyePerp) * pow( dot(eyePerp, eyePerp) * dot(lightPerp, lightPerp), -0.5);
	float cosThetaD = cos((asin(2* uv1.r - 1) - asin(2 * uv1.g - 1))/2);
	
	vec4 M;
	vec3 N;
	M = texture(MarschnerM, vec2(sinThetaI * 0.5 + 0.5, sinThetaO * 0.5 + 0.5));
	//M = texture(MarschnerM, vec2(abs(sinThetaI), abs(sinThetaO)));
	//N = texture(MarschnerN, vec2(cosPhiD * 0.5 + 0.5, M.a * 0.5 + 0.5)).rgb;
	N = texture(MarschnerN, vec2(cosPhiD * 0.5 + 0.5, cos((asin(2* uv1.r - 1) - asin(2 * uv1.g - 1))/2) * 0.5 + 0.5)).rgb;
	//N = texture(MarschnerN, vec2(cosPhiD * 0.5 + 0.5, M.a * 0.5 + 0.5)).rgb;
	
	
	float diffCoeff = 0.6, specularCoeff = 0.5, phong = 100;
	if(dot(t,l)*dot(t,e)>0)
		specular = 0;
	else
		{
			specular = specularCoeff * pow(cos(acos(-1.0 + abs(dot(t,l)) *2) - acos(-1.0 + abs(dot(t,e)) * 2)), phong);
		}
		
	diffuse = diffCoeff * sqrt(1 - dot(t, l) * dot(t, l));
	
	//oColor.rgb = lightColor * (diffuse + specular)*exp(-5.5 * opacity);
	//oColor.rgb = lightColor * (diffuse + specular);
	//oColor.rgb = lightColor * diffuse;
	//oColor.rgb = lightColor * specular * 3;
	//oColor.rgb = vec3(1.0, 1.0, 0.0);
	
	//oColor.rgb = (lightColor * (M.r * N.r + M.g * N.g + M.b * N.b) * 5 + lightColor * diffuse) *exp(-0.8 * opacity);
	oColor.rgb = (lightColor * (M.r * N.r + M.g * N.g + M.b * N.b) * 5 + lightColor * diffuse);
	//oColor.rgb = lightColor * (M.r * N.r + M.g * N.g + M.b * N.b) * 10;
	
	oColor.a = 1.0;	
	
}